package com.example.bootexecutor.task;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;

import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Future;

@Component
@Slf4j
public class TestTask {

    public static Random random = new Random();

    @Async("taskExecutor")
    public void doTaskOne(CountDownLatch cdl5){
        log.info("开始做任务⼀");
        long start = System.currentTimeMillis();
        try {
            Thread.sleep(random.nextInt(10000));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        log.info("完成任务⼀，耗时：" + (end - start) + "毫秒");
        cdl5.countDown();
        // return new AsyncResult<>("任务⼀完成");
    }

    @Async("taskExecutor")
    public void doTaskTwo(CountDownLatch cdl5){
        log.info("开始做任务⼆");
        long start = System.currentTimeMillis();
        try {
            Thread.sleep(random.nextInt(10000));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        log.info("完成任务⼆，耗时：" + (end - start) + "毫秒");
        cdl5.countDown();
        //return new AsyncResult<>("任务二完成");
    }

    @Async("taskExecutor")
    public void doTaskThree(CountDownLatch cdl5){
        log.info("开始做任务三");
        long start = System.currentTimeMillis();
        try {
            Thread.sleep(random.nextInt(10000));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        log.info("完成任务三，耗时：" + (end - start) + "毫秒");
       // return new AsyncResult<>("任务三完成");
        cdl5.countDown();
    }
}
